#region Using directives

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;

#endregion
using System.Diagnostics;
using HTTPMsgAPI;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;
using System.IO;
using System.Collections.Specialized;
using timers = System.Timers;
using Thinxoft.HttpLib;

namespace HTTPListProc
{
    public class HandlerProxy : IDisposable 
    {
        string url, moniker;
        Process process;
        IRequestHandler instance;
        int id;
        bool disposing = false, disposed = false;

        public HandlerProxy(int id)
        {
            this.id = id;
            
            this.process = Process.Start("Xedus.HttpReqMan.exe");
            this.moniker = string.Format("rh{0}", process.Id);
            this.url = string.Format("ipc://RequestHandlerInstance_{0}/{0}", moniker);
            Thread.Sleep(3000);
            this.instance = (HTTPMsgAPI.IRequestHandler)Activator.GetObject(typeof(HTTPMsgAPI.IRequestHandler), url);

        }

        public HandlerProxy(int id,Process process)
        {
            this.id = id;
            this.moniker = string.Format("rh{0}", process.Id);
            this.url = string.Format("ipc://RequestHandlerInstance_{0}/{0}", moniker);
            this.process = process;
            this.instance = (HTTPMsgAPI.IRequestHandler)Activator.GetObject(typeof(HTTPMsgAPI.IRequestHandler), url);
        }

        
        public void Dispose()
        {
            if (process != null)
            {
                disposing = true;
                process.Kill();
                process = null;
                instance = null;
            }
            else
            {
                disposed = true;
            }
        }

        public int ID
        {
            get
            {
                return this.id;
            }
        }

        public string HandlerMoniker
        {
            get
            {
                return this.moniker;
            }
        }

        public Process HandlerPocess
        {
            get
            {
                return this.process;
            }
        }

        public IRequestHandler HandlerInstance
        {
            get
            {
                return this.instance;
            }
        }

        public string HandlerUrl
        {
            get
            {
                return this.url;
            }
        }
    }

    
}
